package com.xuyuan.excel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import lombok.Data;

import java.util.Arrays;
import java.util.List;

public class EasyExcelMultiSheetExample {

    public static void main(String[] args) {
        String fileName = "multiple_sheets.xlsx";  // 输出的文件名

        List<Data1> sheet1Data = getDataForSheet1();
        List<Data2> sheet2Data = getDataForSheet2();
        List<Data3> sheet3Data = getDataForSheet3();

        // 多个sheet写入一个文件
        try (ExcelWriter excelWriter = EasyExcel.write(fileName).build()) {

            // 写入 Sheet1
            WriteSheet writeSheet1 = EasyExcel.writerSheet("Sheet1").head(Data1.class).build();
            excelWriter.write(sheet1Data, writeSheet1);

            // 写入 Sheet2
            WriteSheet writeSheet2 = EasyExcel.writerSheet("Sheet2").head(Data2.class).build();
            excelWriter.write(sheet2Data, writeSheet2);

            // 写入 Sheet3
            WriteSheet writeSheet3 = EasyExcel.writerSheet("Sheet3").head(Data3.class).build();
            excelWriter.write(sheet3Data, writeSheet3);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 模拟获取 Sheet1 数据
    private static List<Data1> getDataForSheet1() {
        return Arrays.asList(new Data1("Alice", 25), new Data1("Bob", 30));
    }

    // 模拟获取 Sheet2 数据
    private static List<Data2> getDataForSheet2() {
        return Arrays.asList(new Data2("Cat", "Brown"), new Data2("Dog", "Black"));
    }

    // 模拟获取 Sheet3 数据
    private static List<Data3> getDataForSheet3() {
        return Arrays.asList(new Data3("John", 2024), new Data3("Emily", 2025));
    }

    @Data
    public static class Data1 {
        private String name;
        private int age;

        public Data1(String name, int age) {
            this.name = name;
            this.age = age;
        }

    }

    @Data
    public static class Data2 {
        private String animal;
        private String color;

        public Data2(String animal, String color) {
            this.animal = animal;
            this.color = color;
        }
    }

    @Data
    public static class Data3 {
        private String name;
        private int year;

        public Data3(String name, int year) {
            this.name = name;
            this.year = year;
        }
    }
}
